/**
 * cTreeSelector 1.0 | colinlin1982@hotmail.com
 * Copyright (c) 2006 Nicholas Lin
 * This script can be use freely as long as all copyright message are intact
 * Updated: 30.03.2007
 *
 * Base on:
 * dTree 2.05 | www.destroydrop.com/javascript/tree/
 * Copyright (c) 2002-2003 Geir Landr? 
 * This script can be used freely as long as all copyright message are intact
 * Updated: 17.04.2003
 *
 * Must have cTreeTable version 2.03 or higer
 * Must have StringBuffer
 */
document.scripts[0].src = "stringBuffer.js";
document.scripts[1].src = "ctreetable.js";

function getLayerInString() {
	var buffer = new StringBuffer();
	// append div str
	buffer.append("<div id='cTreeSelectorLayer' style='position: absolute; z-index: 9999;");
	buffer.append(" left: 0; top: 0; display: none'>");
	buffer.append("<table width='100%' height='100%' cellspacing='0' class='selectortable'><tr id='ckOperRow'>");
	buffer.append("<td height='1%' align='right' class='ckoperate'>");
	buffer.append("<input type='button' class='ckbutton' value='OK' onclick='returnValue();' />");
	buffer.append("</td></tr><tr>");
	buffer.append("<td height='99%'>");
	buffer.append("<iframe name='cTreeSelectorIframe' class='selectorframe' scrolling='yes' frameborder=0></iframe>");
	buffer.append("</td></tr></table></div>");
	return buffer.toString();
}

/*an instance is avialable*/
var cTreeSelector = null;

function getTreeSelector(treeObjName, divId, inputName) {
	var caller = window.event.srcElement;
	if(!cTreeSelector) {
		eTemp = document.getElementById(divId);
		eTemp.insertAdjacentHTML('afterEnd', getLayerInString());
		cTreeSelector = new CTreeSelector(caller, treeObjName);
	} else {
		cTreeSelector.eventSrc = caller;
		// checker or selector
		if(cTreeSelector.useChecks != treeObjName.config.useChecks) {
			cTreeSelector.useChecks = treeObjName.config.useChecks;
			cTreeSelector.rewrite = true;
		}
		if(cTreeSelector.treeObj != treeObjName) {
			cTreeSelector.treeObj = treeObjName;
			cTreeSelector.rewrite = true;
		}
	}
	if(inputName) {
		var inputs = document.getElementsByName(inputName);
		if(!inputs || inputs.length==0) {
			cTreeSelector.valueSrc = document.getElementById(inputName);
		} else {
			cTreeSelector.valueSrc = inputs[0];
		}
	}
	return cTreeSelector;
}

function returnValue() {
	if(cTreeSelector) {
		cTreeSelector.returnValue();
	}
}

function CTreeSelector(caller, treeObjName, valueField) {
	this.config = {
		width		: 160,
		height		: 240,
		aspectRadio	: 1.5,
		aspectWidth	: 180
	};
	this.cssfile = 'ctreeselector.css';
	this.eventSrc = caller;
	this.valueSrc = valueField;
	this.treeObj = treeObjName;
	this.useChecks = this.treeObj.config.useChecks;
	this.rewrite = true;
	this._leftPos = 0;
	this._topPos = 0;
	this.frame = window.frames('cTreeSelectorIframe');
	this.layer = document.getElementById('cTreeSelectorLayer');
};

CTreeSelector.prototype.show = function() {
	if(this.rewrite) {
		// event on node select
		if(!this.useChecks) {
			// selector
			this.treeObj.onRowClick = returnValue;
			document.getElementById('ckOperRow').style.display = 'none';
		} else {
			// checker
			this.treeObj.onRowClick = null;
			document.getElementById('ckOperRow').style.display = 'block';
		}
		this.treeObj.containerFrame = this.frame;
		with(this.frame) {
			document.writeln(this._buildTree());
			document.close();
		}
		this.rewrite = false;
	}
	if(this.valueSrc) {
		// checker
		if(this.useChecks) {
			this.treeObj.setCheckedIds(this.valueSrc.value.split(','));
		// selector
		} else if(this.valueSrc.value) {
			this.treeObj.openTo(this.valueSrc.value, true);
		}
	}
	
	// resize by eventSrc
	this.config.width = this.eventSrc.offsetWidth;
	this.config.height = (this.config.width>this.config.aspectWidth) ? 
							this.config.aspectWidth * this.config.aspectRadio : 
							this.config.width * this.config.aspectRadio;
	this._calculateLocation();
	
	this.layer.style.display = 'block';
};

CTreeSelector.prototype.hide = function() {
	this.layer.style.display = 'none';
}

CTreeSelector.prototype.returnValue = function() {
	var oldValue = this.valueSrc.value;
	if(this.useChecks) {
		// checker
		this.valueSrc.value = this.treeObj.getCheckedIds();
	} else {
		// selector
		this.eventSrc.value = this.treeObj.getSelectedByName();
		this.valueSrc.value = this.treeObj.getSelected();
	}
	this.hide();
	if(oldValue != this.valueSrc.value) {
		this.onChange();
	}
}

CTreeSelector.prototype._buildTree = function() {
	var buffer = new StringBuffer();
	buffer.append("<html><head><meta http-equiv='Content-Type' content='text/html; charset=gb2312'>");
	buffer.append("<link rel='StyleSheet' href='");
	buffer.append(this.cssfile);
	buffer.append("' type='text/css' />");
	buffer.append("</head><body class='ctreeselector'>");
	buffer.append("<div><table align='left' cellspacing=0 width='100%'>");
	buffer.append("<script lanuage='javascript'>var ");
	buffer.append(this.treeObj.obj);
	buffer.append(" = parent.");
	buffer.append(this.treeObj.obj);
	buffer.append("; document.write(");
	buffer.append(this.treeObj.obj);
	buffer.append("); </script></table></div></body></html>");
	return buffer.toString();
}

CTreeSelector.prototype._calculateLocation = function() {
	//  document.body position
	var bodyWidth = document.body.clientWidth;
	var bodyScrollLeft = document.body.scrollLeft;
	var bodyScrollTop = document.body.scrollTop;
	// event src position
	var eventOffsetTop = this.eventSrc.offsetTop;
	var eventOffsetLeft = this.eventSrc.offsetLeft;
	var eventClientHeight = this.eventSrc.offsetHeight;
	// count offset top and left
	evtSrc = this.eventSrc;
	while(evtSrc=evtSrc.offsetParent) {
		eventOffsetTop += evtSrc.offsetTop;
		eventOffsetLeft += evtSrc.offsetLeft;
		if(evtSrc.tagName=='TD') {
			eventOffsetTop += evtSrc.clientTop;
			eventOffsetLeft += evtSrc.clientLeft;
		}
	}
	//  location and size
	this.layer.style.width = this.config.width;
	this.layer.style.height = this.config.height;
	if(document.body.clientHeight + bodyScrollTop - eventOffsetTop - eventClientHeight >= this.config.height) {
		this.layer.style.top = eventClientHeight + eventOffsetTop;
	} else {
		if(eventOffsetTop - bodyScrollTop < this.config.height) {
			this.layer.style.top = eventClientHeight + eventOffsetTop;
		} else {
			// pop upside
			this.layer.style.top = eventOffsetTop - this.config.height;
		}
	}
	if(bodyWidth + bodyScrollLeft - eventOffsetLeft >= this.config.width) {
		this.layer.style.left = eventOffsetLeft;
	} else {
		if(bodyWidth >= this.config.width) {
			this.layer.style.left = bodyWidth - this.config.width + bodyScrollLeft;
		} else {
			this.layer.style.left = bodyScrollLeft;
		}
	}
};

CTreeSelector.prototype.onChange = function() {
};

function selectorHideOnEvent() {
	if(cTreeSelector && cTreeSelector.eventSrc != window.event.srcElement) {
		cTreeSelector.hide();
	}
}

document.onclick = selectorHideOnEvent;
